package com.loan.market.ui

import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.loan.market.R
import com.loan.market.base.BaseBindingActivity
import com.loan.market.bean.MotivePromotion
import com.loan.market.databinding.ActivityPromotionsBinding
import com.loan.market.http.ApiService
import com.loan.market.http.getHttpMap
import com.loan.market.http.request
import com.loan.market.util.afEvent
import com.loan.market.util.http
import com.loan.market.util.jump
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class PromotionsActivity : BaseBindingActivity<ActivityPromotionsBinding>() {

    private var page = 1

    override fun getViewBinding(): ActivityPromotionsBinding {
        return ActivityPromotionsBinding.inflate(layoutInflater)
    }

    override fun initUi(savedInstanceState: Bundle?) {
        afEvent("enter_promotions")
        binding.rvPromotions.adapter = adapter
        binding.refresh.autoRefresh(500)
    }

    override fun initEvent() {
        binding.refresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                getData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                getData()
            }
        })
        adapter.setOnItemClickListener { adapter, view, position ->
            jump(adapter.getItem(position)?.clickTo)
        }
    }

    private val adapter by lazy {
        object : BaseQuickAdapter<MotivePromotion, QuickViewHolder>() {
            override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: MotivePromotion?) {
                holder.setText(R.id.tv_day, item?.publishYmd)
                holder.setText(R.id.tv_content, item?.compaignText)
                holder.setText(R.id.tv_time, item?.publishHms)
                if (position == 0 || !item?.publishYmd.isNullOrEmpty() && item?.publishYmd != getItem(position - 1)?.publishYmd) {
                    holder.setGone(R.id.tv_day, false)
                } else {
                    holder.setGone(R.id.tv_day, true)
                }
            }

            override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
                return QuickViewHolder(R.layout.item_promotions, parent)
            }
        }
    }

    private fun getData() {
        http(onFinish = {
            binding.refresh.finish()
        }) {
            val map = getHttpMap(301)
            map["currentPage"] = page
            map["pageSize"] = 10
            val list = request<List<MotivePromotion>>(map, ApiService.path5)
            if (page == 1) {
                adapter.submitList(list)
            } else {
                adapter.addAll(list ?: listOf())
            }
        }
    }

    override fun initData() {

    }
}